// pages/equipment/index.ts
import request from '../../utils/request'
import Dialog from '@vant/weapp/dialog/dialog'
const app = getApp<IAppOption>()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        globalData: {},

        deviceList: [],// 绑定的设备
        allSelected: false,// 全选
        selectData: [],// 选中的数据
        
        popup: {},// 提示框

        equipmentNo: "",// 设备号

    },

    /**
     * 全选/取消全选
     * @param e 
     */
    selectAll(e: any) {
        const deviceList = this.data.deviceList
        let values:any = [];
        if(! this.data.allSelected){  //全选
            deviceList.forEach( obj => {
                obj.checked = true;
                values.push(obj.equipmentNo);
            })
        } else {  //取消全选，即全不选
            deviceList.forEach( obj => {
                obj.checked = false
            })
        }

        this.setData({
            allSelected: !this.data.allSelected,
            deviceList: deviceList,
            selectData: values
        })
    },
    /**
     * 选中/取消选中设备
     * @param e 
     */
    checkboxChange(e: any) {
        const values = e.detail.value
        // 判断是否全选
        let flag = false;
        if(values && values.length === this.data.deviceList.length) {
            flag = true;
        }
        this.setData({
            allSelected: flag,
            selectData: values
        })
    },
    // 加载绑定的设备
    getData(){
        let that = this;
        request.get('/applet/bind-info/list?dataType='+app.globalData.dataType).then((res: any) => {
            if(res.data.code === 200) {
                let list:any = [];
                let data = res.data.data;
                if(data && data.length > 0) {
                    data.forEach(val => {
                        list.push({
                            checked: false,
                            equipmentNo: val,
                        });
                    });
                }
                that.setData({
                    deviceList: list
                });
            }
        })
    },

    doSubmit(){
        let that = this;
        let data = that.data.selectData
        if(data.length <= 0) {
            that.setData({
                popup: {
                    idx: Math.random(),
                    show: true,
                    title: '提 示',
                    msg: '请先选择设备再解绑！',
                    icon: '../../static/img/no_happy.png'
                },
            })
            return ;
        }

        Dialog.confirm({
            title: '确认要解绑选中的设备吗？',
            confirmButtonColor: "#00A991"
        }).then(() => {
            request.post('/applet/bind-info/unbind', data).then((res: any) => {
                that.setData({
                    popup: {
                        idx: Math.random(),
                        show: true,
                        title: '提 示',
                        msg: '设备解绑成功！',
                        icon: '../../static/img/smile.png'
                    },
                })
                that.getData()
            })
        }).catch(() => {
            // on cancel
        });
    },

    // 点击扫码
    handleScan() {
        wx.scanCode({
            success: (res) => {
                let that = this;
                if(res.result) {
                    that.doAdd(res.result);
                } else {
                    wx.showToast({
                        title: '二维码内容为空',
                        icon: 'error',
                        duration: 2000
                    })
                }
            }
        })
    },

    addEquipment(){
        if(this.data.equipmentNo) {
            this.doAdd(this.data.equipmentNo);
        } else {
            this.setData({
                popup: {
                    idx: Math.random(),
                    show: true,
                    title: '提 示',
                    msg: '请先输入设备号再绑定！',
                    icon: '../../static/img/no_happy.png'
                },
            })
        }

    },

    doAdd(equipmentNo: string){
        let that = this;
        request.post('/applet/bind-info/add', { equipmentType: app.globalData.dataType, equipmentNo: equipmentNo }).then((res: any) => {
            that.setData({
                popup: {
                    idx: Math.random(),
                    show: true,
                    title: '提 示',
                    msg: '设备绑定成功！',
                    icon: '../../static/img/smile.png'
                },

                equipmentNo: '',
            })

            that.getData()
        })
    },
    
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.setData({
            globalData: app.globalData
        });

        this.getData();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})